Haskell Yoneda,CoYoneda
米田の補題の余米田のHaskellでの定義
理解しようと思うと相当な量の知識が必要っぽい
Data.Functor.YonedaパッケージのYoneda
code:Yoneda.hs
{-# LANGUAGE Rank2Types #-}
-- | @Yoneda f a@ can be viewed as the partial application of 'fmap' to its second argument.
newtype Yoneda f a = Yoneda { runYoneda :: forall b. (a -> b) -> f b }
ref: https://hackage.haskell.org/package/kan-extensions-5.2.6/docs/src/Data.Functor.Yoneda.html#Yoneda
forallはランクN多相(GADTs)のやつ?
Yonedaは関数の外側にforallの型変数が1つしか出ていないので、Rank2Typesを有効にすれば問題ないらしい
ここでいう関数の外側はforall bのb
runYonedaはfmap :: (a -> b) -> f a -> f bの f aを抜かした形
Data.Functor.CoyonedaパッケージのCoyoneda
code:Coyoneda.hs
{-# LANGUAGE GADTs #-}
{-# LANGUAGE RankNTypes #-}
data Coyoneda f a where
Coyoneda :: (b -> a) -> f b -> Coyoneda f a
ref: https://hackage.haskell.org/package/kan-extensions-5.2.6/docs/src/Data.Functor.Coyoneda.html#Coyoneda
data Coyoneda f a where Coyoneda :: ...という書き方もGADTs由来の記法
Coyonedaはb部分を見たときにn個の型が問題に
存在型
Coyoneda :: (b -> a) -> f b -> Coyoneda f aをa = Int, b = Intで具体化してみる。
code:Coyoneda.hs
(Int -> Int) -> f Int -> Coyoneda f Int
fにはMaybe、リストの[]とかIdentityなどが入る
Maybe なら
(Int -> Int) -> Maybe Int -> Coyoneda Maybe Int
[] なら
(Int -> Int) -> [Int] -> Coyoneda [] Int
Identity なら
(Int -> Int) -> Identity Int -> Coyoneda Identity Int
Functor が無い型でも (Int -> Int) -> f Int -> Coyoneda f Int は組める
確認用
Q.
関連
Lens
Freer monad
参考
Freer Effectsが、だいたいわかった: 5. 一般化代数データ型(GADTs拡張)の解説 #Haskell - Qiita
メモ
YonedaとCoYoneda、そしてFunctor - capriccioso String Creating(Object something){ return My.Expression(something); }
Introduction to Yoneda and Coyoneda
Haskellのforallについて理解したことを書いておく(ランクN多相限定)。 - uehaj's blog
7.12. 型システムへのその他の拡張
haskell - Step by Step / Deep explain: The Power of (Co)Yoneda (preferably in scala) through Coroutines - Stack Overflow
Haskell/GADT - Wikibooks, open books for an open world
HaskellのRank2Typesがだいぶわかるやつ #ExistentialQuantification - Qiita
Haskell/存在量化された型 - Wikibooks
#Yoneda #Coyoneda
#Haskell #圏論